<template>
  <div>
    <InfoTable ref="InfoTable" location="branches/search/allDetail" :param="param">
      <template slot="button-operation" slot-scope="row">
        <div></div>
      </template>
      <template slot="table-operation" slot-scope="row">
        <b-btn variant="outline-primary" style="margin-right:8px;padding:6px;font-size:0.7rem" @click="showLastBranch(row)">查看基于分支</b-btn>
        <b-btn variant="outline-primary" style="margin-right:8px;padding:6px;font-size:0.7rem" @click="info(row)">详细信息</b-btn>
        <b-btn variant="outline-primary" style="margin-right:8px;padding:6px;font-size:0.7rem" @click="setMasterBranch(row)">改为主分支</b-btn>
        <b-btn variant="outline-primary" style="margin-right:8px;padding:6px;font-size:0.7rem" @click="remove(row)">删除</b-btn>
      </template>
    </InfoTable>
  </div>
</template>

<script>
import InfoModal from "@/components/InfoModal";
import InfoTable from "@/components/InfoTable";
import {
  baseURL,
  get,
  save,
  associate,
  getAndAssociate,
  remove,
  update
} from "@/service/api";
export default {
  components: {
    InfoTable,
    InfoModal
  },
  data() {
    return {
      filterFields: ["status", "type"],
      currentDetail: {},
      currentDetailKey: -1,
      param: -1
    };
  },
  created() {
    this.currentDetail = JSON.parse(this.$route.query.entry);
    this.detailKey = parseInt(this.getKey(this.currentDetail._links.self.href));
    this.param = "detailId=" + this.detailKey;
  },
  methods: {
    showLastBranch: async function(row) {
      console.log(row.data.item._links.lastBranch.href);
      let record = {
        item: {}
      };
      await get(row.data.item._links.lastBranch.href)
        .then(reponse => {
          if (reponse.status == "200") {
            console.log("资源获取成功");
            record.item = reponse.data;
            this.$refs.InfoTable.info(record);
          }
          console.log(reponse);
        })
        .catch(error => {
          alert("此分支无父分支或父分支已经被删除");
        });
    },
    setMasterBranch: async function(row) {
      let self = this;
      let record = row.data.item;
      console.log(record);
      if (record.isMaster) {
        alert("已经是主分支了");
      } else {
        let param = "detailId=" + this.detailKey;
        await get(`branches/search/clearMaster?${param}`)
          .then(reponse => {
            if (reponse.status == "200") {
              console.log("清除MasterBranch成功");
            }
            console.log(reponse);
          })
          .catch(error => {
            console.log(error);
          });
        await update(record._links.self.href, {
          isMaster: true
        })
          .then(reponse => {
            if (reponse.status == "200") {
              console.log("资源修改成功");
              alert("设置成功");
              self.$refs.InfoTable.refresh();
            }
            console.log(reponse);
          })
          .catch(error => {
            console.log(error);
          });
      }
    },
    info(row) {
      console.log(row);
      this.$refs.InfoTable.info(row.data);
      this.$refs.InfoTable.refresh();
    },
    getKey(str) {
      let position = str.lastIndexOf("/");
      return str.substring(position + 1, str.length);
    },
    remove(row) {
      let record = row.data.item;
      console.log(record);
      if (record.isMaster) {
        alert("无法删除主分支");
      } else {
        this.$refs.InfoTable.remove(row.data);
      }
    }
  }
};
</script>
<style lang="scss">
</style>
